﻿/*****************************************介绍*****************************************
 * 作者：24531
 * 创建时间：2025-08-27 22:42:52
 * 功能：界面【SettingPanel】的View
        1、负责收集所有控件
**************************************************************************************/

using Game.Framework;
using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.SettingPanel
{
    public class SettingPanelView : PopupPanel
    {
        public const string PANEL_KEY = "Game_UI_SettingPanel_SettingPanel_Key";

        protected override void RegistAny()
        {
            PanelManager.Instance.RegistPanel(PANEL_KEY, this);
        }

        protected override void UnregistAny()
        {
            PanelManager.Instance.UnregistPanel(PANEL_KEY);
        }

        protected override void OnEnable()
        {
            base.OnEnable();
            GetControl<Button>(SettingPanelNotifications.RestoreBtn_CTRL).interactable = !GameDataMgr.Instance.IsResotred;
            GetControl<Toggle>(SettingPanelNotifications.MusicSwitch_CTRL).SetIsOnWithoutNotify(GameDataMgr.Instance.MusicSwitch);
            GetControl<Toggle>(SettingPanelNotifications.SoundSwitch_CTRL).SetIsOnWithoutNotify(GameDataMgr.Instance.SoundSwitch);
            GetControl<Toggle>(SettingPanelNotifications.VibrationSwitch_CTRL).SetIsOnWithoutNotify(GameDataMgr.Instance.VibrationSwitch);
            GetControl<Text>(SettingPanelNotifications.Uid_CTRL).text = $"UID:{GameDataMgr.Instance.Uid}";
        }

        protected override void ClickBtnHandle(string name)
        {
            switch (name)
            {
                case SettingPanelNotifications.CloseBtn_CTRL:
                    UIUtility.HidePanel(PANEL_KEY);
                    break;
                case SettingPanelNotifications.PrivacyBtn_CTRL:
                    Application.OpenURL("https://www.nicolegames.com/pp.html");
                    break;
                case SettingPanelNotifications.LanguageBtn_CTRL:
                    LogManager.Log($"切换多语言，TODO：");
                    break;
                case SettingPanelNotifications.RestoreBtn_CTRL:
                    if (GameDataMgr.Instance.Purchase is IAP)
                    {
                        (GameDataMgr.Instance.Purchase as IAP).RestoreTransactions();
                        GetControl<Button>(SettingPanelNotifications.RestoreBtn_CTRL).interactable = false;
                    }
                    break;
            }
        }

        protected override void ClickToggleHandle(string name, bool isOn)
        {
            switch (name)
            {
                case SettingPanelNotifications.MusicSwitch_CTRL:
                    AudioManager.Instance.ChangeBgVolume(isOn ? BaseDataMgr.Instance.Cfg.bgVolume : 0);
                    GameDataMgr.Instance.MusicSwitch = isOn;
                    break;
                case SettingPanelNotifications.SoundSwitch_CTRL:
                    AudioManager.Instance.ChangeAudioVolume(isOn ? 1 : 0);
                    GameDataMgr.Instance.SoundSwitch = isOn;
                    break;
                case SettingPanelNotifications.VibrationSwitch_CTRL:
                    GameDataMgr.Instance.VibrationSwitch = isOn;
                    break;
            }
        }
    }
}